/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */

package org.jeecg.modules.third.jg.report.freight.information.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.third.jg.report.freight.information.constant.HubeiReportConstant;
import org.jeecg.modules.third.jg.report.freight.information.constant.YunnanReportConstant;
import org.jeecg.modules.third.jg.report.freight.information.constant.enums.ReportDocTypeEnum;
import org.jeecg.modules.third.jg.report.freight.information.dto.AppInfoDTO;
import org.jeecg.modules.third.jg.report.freight.information.model.ExchangeModel;
import org.jeecg.modules.third.jg.report.freight.information.model.R;
import org.jeecg.modules.third.jg.report.freight.information.util.WebUtils;
import org.jeecg.modules.third.jg.report.freight.information.util.sm.SM2Util;
import org.jeecg.modules.third.jg.report.freight.information.util.sm.SM4Util;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description: 云南平台上报服务
 */
@Service
@Slf4j
public class YunnanReportPlatformService {

    @Resource
    private RedisUtil redisUtil;

    private String reportPlatformDomain;

    private String username;

    private String password;

    private String publickey;

    private String appId;

    private String appSecurity;

    private String appSenderCode;

    private String appDebugSystem;

    private String senderCode = "530000";

    private String eSenderCode;

    public AppInfoDTO getAppInfo() {
        return AppInfoDTO.builder()
                .appId(this.appId)
                .appSecurity(this.appSecurity)
                .enterpriseSenderCode(this.appSenderCode)
                .debugSystem(this.appDebugSystem)
                .build();
    }


    public void setReportInfo(String reportPlatformDomain,
                              String username,
                              String password,
                              String publickey,
                              String appId,
                              String appSecurity,
                              String appSenderCode,
                              String appDebugSystem) {
        //联调状态的App需要设置debugSystem
        if ("0".equals(appDebugSystem)) {
            appDebugSystem = "debugSystem";
        }
    }

    private String getLoginToken() {
        return StringUtils.EMPTY;
    }

    /**
     * 组装交换报文
     * @param reportDocTypeEnum 上报单据类型
     * @param jsonString 报文内容
     * @return
     */
    private ExchangeModel messageAssembly(ReportDocTypeEnum reportDocTypeEnum , String jsonString) throws Exception {
        return null;
    }

    /**
     * 发送上报
     * @param reportDocTypeEnum
     * @param jsonString
     */
    public String sendReport(ReportDocTypeEnum reportDocTypeEnum, String jsonString) throws Exception {
        String sendResult = StringUtils.EMPTY;
        try {
            ExchangeModel exchangeModel = messageAssembly(reportDocTypeEnum, jsonString);
            if (exchangeModel != null) {
                //发送报文
                String url = this.reportPlatformDomain + YunnanReportConstant.SEND_URL;
                log.info("云南监管数据请求-> {} - {}", url, jsonString);
                sendResult = HttpUtil.post(url, JSONUtil.toJsonStr(exchangeModel));
                log.info("云南监管数据回执<- {}", sendResult);
                if (StringUtils.isNotEmpty(sendResult)) {
                    R result = JSON.parseObject(sendResult, R.class);
                    //TOKEN 失效，同步删除自己的缓存后，用户再重新上传
                    if(result.getCode() == 2001){
                        redisUtil.del(YunnanReportConstant.API_REPORT_TOKEN_KEY);
                    }
                    if (result.getCode() == 1001) {
                        return sendResult;
                    }
                }
            }
        } catch (Exception e) {
            log.info("云南监管上报异常:{}", e.getMessage());
            throw e;
        }

        return sendResult;
    }

}
